package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.VerifyService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class PictureCheckCodeServlet extends HttpServlet {
    private LogService logService;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        //创建对象
        VerifyService vc = new VerifyService();
        //获取图片对象
        BufferedImage bi = vc.getImage();
        //获得图片的文本内容
        String text = vc.getText();
        // 将系统生成的文本内容保存到session中
        session.setAttribute("text", text);

        logService = new LogService();
        logService.insertLog("VerifyCode","Display/Flush VerifyCode!", "null");
        //向浏览器输出图片
        vc.output(bi, response.getOutputStream());

    }
}
